package com.yxw.springmvc.userBehavior.util;


import java.util.*;

/**
 * ClassName 判断对象是否为空
 * Author ChiFa Vane
 * Date 2014/10/16
 */
public class CollectionUtils {
    /**
     * 判断对象是否为空
     *
     * @param object obj
     * @return true空，false不为空
     */
    public static boolean isEmpty(Object object) {
        return (object == null);
    }

    /**
     * 判断Boolean是否为true
     *
     * @param b obj
     * @return true空，false不为空
     */
    public static boolean isEmpty(Boolean b) {
        return (b == false);
    }

    /**
     * 判断string是否为空
     *
     * @param string string
     * @return true空，false不为空
     */
    public static boolean isEmpty(String string) {
        return (string == null || "".equals(string));
    }

    /**
     * 判断Long值是否为空或者小于1
     *
     * @param l ID
     * @return true空，false不为空
     */
    public static boolean isEmpty(Long l) {
        return (l == null || l < 1L);
    }

    /**
     * 判断Long值是否为空或者小于1
     *
     * @param i ID
     * @return true空，false不为空
     */
    public static boolean isEmpty(Integer i) {
        return (i == null || i <= 0L);
    }

    /**
     * 判断Collection是否为空
     *
     * @param collection obj
     * @return true空，false不为空
     */
    public static boolean isEmpty(Collection collection) {
        return (collection == null || collection.isEmpty());
    }

    /**
     * 判断Map是否为空
     *
     * @param map obj
     * @return true空，false不为空
     */
    public static boolean isEmpty(Map map) {
        return (map == null || map.isEmpty());
    }


    /**
     * 获取两个集合的不同元素
     *
     * @param collectionMax
     * @param collectionMin
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static Collection getDifferent(Collection collectionMax, Collection collectionMin) {
        //使用LinkedList防止差异过大时,元素拷贝
        Collection csReturn = new LinkedList();
        Collection max = collectionMax;
        Collection min = collectionMin;
        //先比较大小,这样会减少后续map的if判断次数
        if (collectionMax.size() < collectionMin.size()) {
            max = collectionMin;
            min = collectionMax;
        }
        //直接指定大小,防止再散列
        Map<Object, Integer> map = new HashMap<Object, Integer>(max.size());
        for (Object object : max) {
            map.put(object, 1);
        }
        for (Object object : min) {
            if (map.get(object) == null) {
                csReturn.add(object);
            } else {
                map.put(object, 2);
            }
        }
        for (Map.Entry<Object, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                csReturn.add(entry.getKey());
            }
        }
        return csReturn;
    }

    /**
     * 获取两个集合的不同元素,去除重复
     *
     * @param collectionMax
     * @param collectionMin
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static Collection getDifferentNoDuplicate(Collection collectionMax, Collection collectionMin) {
        return new HashSet(getDifferent(collectionMax, collectionMin));
    }
}
